Option Explicit On 
Imports System.Management
Imports TBVLibrary.LibDataType.STATUS_CODE

Public Class Library


    Public Shared Function TBVComputersON(ByRef _Computers As classComputer) As Boolean
        Dim bRet As Boolean = False
        Dim objSC As ManagementScope
        Dim objComputer As Computer
        Dim strScope As String
        Dim objConnectionOption As ConnectionOptions

        If _Computers.ItemCount > 0 Then
            For Each objComputer In _Computers

                objConnectionOption = New ConnectionOptions
                objConnectionOption.Username = objComputer.userName
                objConnectionOption.Password = objComputer.userPassword

                strScope = "\\" & objComputer.Name & "\root\cimV2"

                objSC = New ManagementScope(strScope, objConnectionOption)
                Call objSC.Connect()
                If objSC.IsConnected Then
                    objComputer.connectionStatus = True
                    bRet = True
                End If
                objSC = Nothing
            Next
        End If

        Return bRet
    End Function



    Public Shared Function WIN32_NetworkAdapterConfiguration(ByVal _Computers As classComputer, _
                                                             ByRef _status As STATUS_CODE, _
                                                             ByRef _Msg As String) As DataTable

        Dim dtReturn As DataTable
        dtReturn = WIN32_QueryProperties("Win32_NetworkAdapterConfiguration", _Computers, _status, _Msg)
        Return dtReturn
    End Function


    Public Shared Function WIN32_OperatingSystem(ByVal _Computers As classComputer, _
                                                 ByRef _status As STATUS_CODE, _
                                                 ByRef _Msg As String) As DataTable


        Dim dtReturn As DataTable
        dtReturn = WIN32_QueryProperties("WIN32_OperatingSystem", _Computers, _status, _Msg)
        Return dtReturn
    End Function





    Private Shared Function WIN32_QueryProperties(ByVal _Win32ClassName As String, _
                                           ByVal _Computers As classComputer, _
                                           ByRef _status As STATUS_CODE, _
                                           ByRef _msg As String) As DataTable

        Dim objOS As ManagementObjectSearcher
        Dim objCols As ManagementObjectCollection
        Dim objDataCols As PropertyDataCollection
        Dim objProperty As PropertyData
        Dim objClass As ManagementClass
        Dim sWQL As String
        Dim objComputer As Computer
        Dim objResult As ManagementObject
        Dim i As Integer
        Dim strPath As String
        Dim dRow As DataRow
        Dim dColumn As DataColumn
        Dim dtReturn As New DataTable
        Dim bFirstCreation As Boolean = False

        If TBVComputersON(_Computers) Then

            For Each objComputer In _Computers

                sWQL = "SELECT * FROM " & _Win32ClassName
                strPath = "\\" & objComputer.Name & "\root\cimV2"

                objOS = New ManagementObjectSearcher(strPath, sWQL)
                objCols = objOS.Get

                For Each objResult In objCols

                    If Not bFirstCreation Then

                        objDataCols = objResult.Properties
                        For Each objProperty In objDataCols
                            If Not dtReturn.Columns.Contains(objProperty.Name) Then
                                dColumn = New DataColumn(objProperty.Name, GetType(System.String))
                                dtReturn.Columns.Add(dColumn)
                            End If
                        Next

                        bFirstCreation = True
                    End If

                    '   fetch all row
                    dRow = dtReturn.NewRow
                    For Each objProperty In objDataCols
                        dRow(objProperty.Name) = objResult.Item(objProperty.Name)
                    Next
                    Call dtReturn.Rows.Add(dRow)
                Next

            Next

            _status = OUTPUT

        Else
            _status = ERR
            _msg = "Computer not found."
        End If

        Return dtReturn

    End Function






End Class
